package com.qzyh.esb.egsa.controller;

import com.qzyh.esb.egsa.bean.vo.DeptTreeVO;
import com.qzyh.esb.egsa.entity.Dept;
import com.qzyh.esb.egsa.service.IDeptService;
import com.qzyh.esb.egsa.utils.FillUtil;
import com.qzyh.esb.egsa.utils.ResultObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author whq
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    private static final Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private IDeptService deptService;

    @GetMapping("/list")
    @ApiOperation(value = "查询部门列表")
    @ApiResponses(value = { @ApiResponse(code = 1, message = "系统繁忙，请稍后重试") })
    public ResultObject<List<DeptTreeVO>> index(@ApiParam(value = "部门名称") @RequestParam(value="deptName",required=false) String deptName) {
        List<DeptTreeVO> deptTreeVOList = deptService.getList(deptName);
        return ResultObject.success(deptTreeVOList);
    }

    @GetMapping("/superior")
    @ApiOperation(value = "获取上级部门数组")
    public ResultObject<List<String>> superior(@ApiParam(value = "部门id") @RequestParam("id") String id) {
        //先获取全量部门数据
        List<DeptTreeVO> deptTreeVOList = deptService.getList(null);
        //再通过部门id查找所有上级部门
        List<String> list = deptService.getFatherTree(deptTreeVOList, id);

        //如果有查到上级部门，则list第一条数据为该部门本身 去掉
        if (list.size() > 0) list.remove(0);

        return ResultObject.success(list);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存部门信息")
    public ResultObject save(@RequestBody Dept dept) {
        logger.info("请求参数："+dept.toString());
        dept = (Dept) FillUtil.fill(dept);
        logger.info("填充后的信息："+dept.toString());
        boolean flag = deptService.saveOrUpdate(dept);
        if (flag) return ResultObject.success("保存成功");
        else return ResultObject.fail("保存失败");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除部门信息")
    public ResultObject deleteById(@ApiParam(value = "部门id") @RequestParam("id") String id) {
        logger.info("删除部门信息id："+id);
        return deptService.deleteById(id);
    }

    @GetMapping("/query")
    @ApiOperation(value = "通过部门名称查询部门信息")
    @ApiResponses(value = { @ApiResponse(code = 1, message = "系统繁忙，请稍后重试") })
    public ResultObject<List<Dept>> query(@ApiParam(value = "部门名称") @RequestParam(value="deptName",required=false) String deptName) {
        List<Dept> deptList = deptService.queryByName(deptName);
        return ResultObject.success(deptList);
    }

    @GetMapping("/queryByIds")
    @ApiOperation(value = "通过id查询部门信息")
    @ApiResponses(value = { @ApiResponse(code = 1, message = "系统繁忙，请稍后重试") })
    public ResultObject<List<Dept>> queryByIds(@ApiParam(value = "部门id集合字符串") @RequestParam("idsStr") String idsStr) {
        List<Dept> deptList = deptService.queryByIds(idsStr);
        return ResultObject.success(deptList);
    }
}
